package Q590

import java.util.*
import kotlin.collections.ArrayDeque
import kotlin.collections.ArrayList

/**
 * n叉树的后续遍历
 * @author23737
 * @date 2022.4.26
 */
class Main {

}


 class Node(var `val`: Int) {
     var children: List<Node?> = listOf()
 }

/**
 * 递归
 */
 class Solution {
    fun postorder(root: Node?): List<Int> {
        val res : MutableList<Int> = ArrayList<Int>()
        if(root == null){
            return res
        }
        helper(res,root)
        return res
    }

    private fun helper(res: MutableList<Int>, root: Node) {
        if(root == null){
            return
        }
        for(i in root.children){
            helper(res, i!!)
        }
        res.add(root.`val`)
    }
}
